package ALBasicServer.ALProcess;

import ALBasicCommon.ALBasicCommonFun;
import ALServerLog.ALServerLog;

/****************************
 * 多个步骤需要处理的时候通过本处理对象进行的过程控制处理
 * 
 * ALDelegateProcess 带入一个Action回调，在流程处理完时，调用回应回调即可继续。这个回调无法返回结果。
 * @author mj
 *
 */
public class ALDelegateProcess extends _AALProcess implements _IALProcessAction
{
    /// <summary>
    /// 创建一个多过程过程对象
    /// </summary>
    /// <returns></returns>
    public static ALDelegateProcess CreateDelegateProcess(String _processTag) { return new ALDelegateProcess(_processTag); }

    /// <summary>
    /// 设置处理操作
    /// </summary>
    private _IALProcessDelegate _m_lDealAction;

    //失败回调
    private _IALProcessAction _m_dFailAction;
    //是否失败的时候也执行完成函数
    private boolean _m_bIsFailContinue;

    protected ALDelegateProcess(String _processTag)
    {
    	super(_processTag);
    	
        _m_lDealAction = null;

        _m_dFailAction = null;
        _m_bIsFailContinue = false;
    }

    /// <summary>
    /// 添加一个执行过程
    /// </summary>
    /// <param name="_process"></param>
    /// <returns></returns>
    public ALDelegateProcess setProcess(_IALProcessDelegate _delegate)
    {
    	return setProcess(_delegate, null, true);
    }
    public ALDelegateProcess setProcess(_IALProcessDelegate _delegate, _IALProcessAction _failDelegate, boolean _isFailContinue)
    {
        if(null == _delegate)
            return this;

        if(isRunning())
        {
            ALServerLog.Error("add process when process started!");
            return this;
        }

        //设置处理
        _m_lDealAction = _delegate;
        _m_dFailAction = _failDelegate;
        _m_bIsFailContinue = _isFailContinue;

        return this;
    }
    
	/**************
	 * 单纯的一个纯函数处理
	 * 
	 * 这里直接处理完成
	 * @param _process
	 */
    @Override
	public void dealAction()
	{
    	_onProcessDone();
	}

    /// <summary>
    /// 获取出来可以在监控对象外围补充输出的信息
    /// </summary>
    @Override
    protected String _processMonitorExInfo()
    {
        return "";
    }

    /// <summary>
    /// 进行处理
    /// </summary>
    @Override
    protected void _dealProcess(_IALProcessMonitor _monitor)
    {
        //无数据则直接完成
        if(null == _m_lDealAction)
        {
            _onProcessDone();
            return;
        }

        //处理过程
        try
        {
            _m_lDealAction.dealAction(this);
        }
        catch (Exception _ex)
        {
            //调用回调
            if (null != _monitor)
            {
                _monitor.onErr(ALBasicCommonFun.getNowTimeMS() - processStartTimeMS(), getFullProcessTag(), _processMonitorExInfo(), _ex);
            }
            else
            {
                ALServerLog.Error("Delegate Process " + getFullProcessTag() + " Fail! " + _processMonitorExInfo());
                ALServerLog.Error(_ex.getMessage());
            }

            //执行失败函数
            if (null != _m_dFailAction)
                _m_dFailAction.dealAction();

            //判断失败是否需要执行成功函数
            if (!_m_bIsFailContinue)
        		//设置进程被中断
            	_m_cController._enable = false;

        	//调用监控对象的失败处理函数
        	if(null != _monitor)
        		_monitor.onProcessFailStop(this);

            //执行成功函数
            _onProcessDone();
        }
    }

    /// <summary>
    /// 当子处理对象完成时的处理
    /// </summary>
    @Override
    protected void _onChildDone()
    {
    }

    /// <summary>
    /// 重置处理
    /// </summary>
    @Override
    protected void _onDiscard()
    {
        _m_lDealAction = null;

        _m_dFailAction = null;
        _m_bIsFailContinue = false;
    }

    /// <summary>
    /// 完成的处理，需要放回缓存池
    /// </summary>
    @Override
    protected void _onDone()
    {
    }
}
